package cn.hcbycloud.common.async.service.pool;

import cn.hcbycloud.common.async.vo.ThreadPool;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author gaoh
 * @desc 线程池抽象类
 * @date 2022年02月06日 下午 10:24
 */
public abstract class BaseThreadPoolService implements IThreadPoolService {
    /**
     * 组装线程池配置信息
     *
     * @return
     */
    @Override
    public ThreadPoolTaskExecutor getThreadPoolProperties() {
        ThreadPool threadPool = this.getThreadPool();
        if (ObjectUtil.isNull(threadPool)) {
            return null;
        } else {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(threadPool.getCorePoolSize());
            executor.setMaxPoolSize(threadPool.getMaxPoolSize());
            executor.setQueueCapacity(threadPool.getQueueCapacity());
            executor.setKeepAliveSeconds(threadPool.getKeepAliveSeconds());
            executor.setThreadNamePrefix(threadPool.getThreadNamePrefix());
            return executor;
        }
    }

    /**
     * 获取线程池配置信息
     *
     * @return
     */
    protected abstract ThreadPool getThreadPool();
}